package com.rtx.edi.platform.rule.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.rule.bean.UpdRule;
import com.rtx.edi.platform.rule.service.UpdRuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"更新规则"})
@RestController
@RequestMapping("/v1/UpdRule")
public class UpdRuleController {
	
	@Autowired
	UpdRuleService updRuleService;
	
	@Autowired
	ApplicationContext context;
	
    
    /**
     * 获取-更新规则
     * @return
     */
    @ApiOperation(value = "分页获取-更新规则", notes = "分页获取-更新规则")
    @PostMapping("selectUpdRulePageList")
    public EdiResponse selectUpdRulePageList(@RequestBody UpdRule dto){
        Page<UpdRule> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        updRuleService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * -更新规则
     * @return
     */
    @ApiOperation(value = "根据ID查询-更新规则", notes = "根据ID查询-更新规则")
    @PostMapping("selectUpdRuleById")
    public EdiResponse selectUpdRuleById(@RequestBody UpdRule dto){
        return EdiResponse.success().data("item",updRuleService.getById(dto.getId()));
    }

    /**
     * 新增-更新规则
     * @return
     */
    @ApiOperation(value = "新增-更新规则", notes = "新增-更新规则")
    @PostMapping("addUpdRule")
    public EdiResponse addUpdRule(@RequestBody UpdRule dto){
    	try {
    		dto.setId(null);
    		updRuleService.save(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改-更新规则
     * @return
     */
    @ApiOperation(value = "修改-更新规则", notes = "修改-更新规则")
    @PostMapping("editUpdRule")
    public EdiResponse editUpdRule(@RequestBody UpdRule dto){
    	try {
    		updRuleService.updateById(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
    	
    }

    /**
     * 批量删除-更新规则
     * @return
     */
    @ApiOperation(value = "批量删除-更新规则", notes = "批量删除-更新规则")
    @PostMapping("deleteUpdRule")
    public EdiResponse deleteUpdRuleByIds(@RequestBody List<UpdRule> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  updRuleService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
}
